const db = wx.cloud.database();
const _ = db.command;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    LandAdministrationAct:[],
    LandContractLaw:[],
    show:false,
    inputList:[],
    value:'',
    paths:'',
  },
  tabSelect:function(e){
    var current = e.currentTarget.dataset.id
    this.setData({
      current:current
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onPullDownRefresh:function () {
    setTimeout(()=>{ wx.stopPullDownRefresh({
    success: (res) => {}})},1000)  
},
  onLoad: function (options) {
      var list1=[];
      var list2=[];
    db.collection("laws").orderBy('section','asc').get().then(res=>{
        res.data.forEach(element => {
            if(element.name == "中华人民共和国土地管理法"){
                list1.push(element)
            }else{
                list2.push(element)
            }
        });

        this.setData({
            LandAdministrationAct:list1,
            LandContractLaw:list2
        })

    })
  },

  preview(path){
      wx.downloadFile({

        url: path.currentTarget.dataset.id,
        success:function(res){
            console.log("youi")
            const filePath = res.tempFilePath;
            wx.openDocument({
              filePath: filePath,
              fileType:'pdf',
              success:function(res){
              },
              fail:function(res){
                   wx.showToast({
                     title: '打开文件失败',
                     icon:"none"
                   })
              }
            })
        },
        fail:function(res){
            console.log(res)

        }
    })
  },
  input(e){

   this.setData({
       value:this.data.inputList[ e.currentTarget.dataset.id].sectionName,
       paths:this.data.inputList[ e.currentTarget.dataset.id].fileid,
   })
   
   this.setData({
    show:false
  })
  },
 blur(){
    this.setData({
        show:false
      })
 },
  clear(){
      this.setData({
          show:false
      })
  },
  onSearch(){
      var path=this.data.paths;
     
      if(this.data.paths!=null){     
    wx.downloadFile({
        url: path,
        success:function(res){
            const filePath = res.tempFilePath;
            wx.openDocument({
              filePath: filePath,
              fileType:'pdf',
              success:function(res){
              },
              fail:function(res){
                   wx.showToast({
                     title: '打开文件失败',
                     icon:"none"
                   })
              }
            })
        }
    })}
  },
  onChange(key){
    this.setData({
        show:true
    })
    this.search(key);
  },

  search(e){
    var key = e.detail;
    console.log(key)
    db.collection('laws').where(
            {
            sectionName:db.RegExp({
                    regexp:'^.*'+key,
                })
            }
        ).get().then(res=>{
            console.log(res);
            this.setData({
                inputList:res.data,
            })
        })
  }

})